<?php defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * @author CHUN Dany <chundany@gmail.com>
 */
class Ads extends Admin_Controller
{
  function __construct()
  {
    parent::__construct();
    $this->load->model('mod_ads', 'md');
  }
  
  public function view()
  {
    $q = getSearchKeyword($this->input->get('q'));
    $uriSegment = 4;
    $perPage = 15;
    $this->data = $this->md->getAds(uri($uriSegment), $perPage, $q);
    pagination_config(site_url(module_controller('view')), $this->md->getAdsNum($q), $perPage, $uriSegment); // base_url, total_num, per_page, segment
    
    $this->title  = 'Ads Management';
    $this->action = 'Ads Management';
    $this->btnActtion = 'new';
    $this->lblActtion = 'Add New';
  }
  
  public function add()
  {
    if($this->input->post())
    {
      $this->form_validation->set_rules('title', 'Title', 'trim|xss_clean|required|max_length[50]')
                            ->set_rules('price', 'Price', 'trim|xss_clean|required|numeric|max_length[20]')
                            ->set_rules('categoryId', 'Category', 'trim|xss_clean|required|integer|max_length[11]')
                            ->set_rules('typeId', 'Type', 'trim|xss_clean|required|integer|max_length[11]')
                            ->set_rules('locationId', 'Location', 'trim|xss_clean|required|integer|max_length[11]')
                            ->set_rules('publishDate', 'Publish Date', 'trim|xss_clean|required|max_length[20]')
                            ->set_rules('description', 'Description', 'trim|xss_clean|required')
                            ->set_rules('shortDescription', 'Short Description', 'trim|xss_clean|required')
                            ->set_rules('disable', 'Disable', 'trim|xss_clean');

      if($this->form_validation->run() === TRUE)
      {
        if($id = $this->md->insert())
        {
          $config['upload_path']  = UPLOAD_PATH . 'ads/';
          $config['overwrite']    = FALSE;
          $config['allowed_types'] = 'gif|jpg|png';

          $files = image_multi_upload('image', $config);
          if($files)
          {
            $ads_image = array();
            foreach($files as $file)
            {
              $imageId = $this->md->insertImage($file['file_name'], $id);

              resize_image($file['file_name'], 75, 75, $config['upload_path'], $config['upload_path'].'75x75/');
              resize_image($file['file_name'], 250, 250, $config['upload_path'], $config['upload_path'].'250x250/');
            }
          }
          redirect(module_controller('detail/' . $id));
        }
        else
        {
          errorMsg('Could not add new, please try again!');
        }
      }
    }
    $this->category = $this->md->getCategoryList();
    $this->type = $this->md->getTypeList();
    $this->location = $this->md->getLocationList();
    
    $this->title  = 'Add New Ads';
    $this->action = 'Add New Ads';
  }
  
  public function edit()
  {
    $id = $this->uri->segment(4);
    $row = $this->md->getAdsById($id);
    
    if($this->input->post())
    {
      $this->form_validation->set_rules('title', 'Title', 'trim|xss_clean|required|max_length[50]')
                            ->set_rules('price', 'Price', 'trim|xss_clean|required|numeric|max_length[20]')
                            ->set_rules('categoryId', 'Category', 'trim|xss_clean|required|integer|max_length[11]')
                            ->set_rules('typeId', 'Type', 'trim|xss_clean|required|integer|max_length[11]')
                            ->set_rules('locationId', 'Location', 'trim|xss_clean|required|integer|max_length[11]')
                            ->set_rules('publishDate', 'Publish Date', 'trim|xss_clean|required|max_length[20]')
                            ->set_rules('description', 'Description', 'trim|xss_clean|required')
                            ->set_rules('shortDescription', 'Short Description', 'trim|xss_clean|required')
                            ->set_rules('disable', 'Disable', 'trim|xss_clean');

      if($this->form_validation->run() === TRUE)
      {
        if($this->md->update($id))
        {
          $config['upload_path']  = UPLOAD_PATH . 'ads/';
          $config['overwrite']    = FALSE;
          $config['allowed_types'] = 'gif|jpg|png';

          $files = image_multi_upload('image', $config);
          if($files)
          {
            $ads_image = array();
            foreach($files as $file)
            {
              $imageId = $this->md->insertImage($file['file_name'], $file);

              resize_image($file['file_name'], 75, 75, $config['upload_path'], $config['upload_path'].'75x75/');
              resize_image($file['file_name'], 250, 250, $config['upload_path'], $config['upload_path'].'250x250/');
            }
          }

          redirect(module_controller('detail/' . $id));
        }
        else
        {
          errorMsg('Could not add new, please try again!');
        }
      }
    }
    $this->category = $this->md->getCategoryList();
    $this->type = $this->md->getTypeList();
    $this->location = $this->md->getLocationList();
    $this->row = $row;
      
    $this->title  = 'Edit Ads';
    $this->action = 'Edit Ads';
  }
  
  public function detail()
  {
    $id = $this->uri->segment(4);
    $this->row = $this->md->getAdsById($id);
      
    $this->title  = 'Ads Detail';
    $this->action = 'Ads Detail';
  }
  
  public function delete()
  {
    $id = $this->uri->segment(4);
    
    if($this->md->delete($id) > 0)
    {
      successMsg('Successfully deleted!');
    }
    else
    {
      errorMsg('Could not delete, please try again!');
    }
    
    redirect(module_controller('view'));
  }
}